package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.AttendanceSetting;
import com.wtwd.campus.service.AttendanceRulesService;
import com.wtwd.campus.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attendanceSetting")
public class AttendanceSetController {
    private static final Logger logger = LoggerFactory.getLogger(AttendanceDeviceController.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private AttendanceRulesService attendanceRulesService;

    /**
     * 根据学校id查询考勤设置
     *
     * @param schoolId
     * @param request
     * @return
     */
    @GetMapping("/getAttendanceSetBySchoolId/{schoolId}/{currPage}/{pageSize}")
    public RespEntity getAttendanceSetBySchoolId(@PathVariable("schoolId") Integer schoolId,@PathVariable("currPage") Integer currPage,
                                                 @PathVariable("pageSize") Integer pageSize,HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/getAttendanceSetBySchoolId,schoolId={},curPage={},pageSize={}", schoolId.toString(),currPage.toString(),pageSize.toString());
        Map<String, Object> result = attendanceRulesService.getAttendanceSetBySchoolId(schoolId,currPage,pageSize);
        return new RespEntity(RespCode.SUCCESS, result);
    }


    /**
     * 根据年级id查询考勤设置
     *
     * @param gradeId
     * @param request
     * @return
     */
    @GetMapping("/getAttendanceSetByGradeId/{gradeId}/{currPage}/{pageSize}")
    public RespEntity getAttendanceSetByGradeId(@PathVariable("gradeId") Integer gradeId,@PathVariable("currPage") Integer currPage,
                                                @PathVariable("pageSize") Integer pageSize,HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/getAttendanceSetByGradeId,gradeId={},curPage={},pageSize={}", gradeId.toString(),currPage.toString(),pageSize.toString());
        Map<String, Object> result = attendanceRulesService.getAttendanceSetByGradeId(gradeId,currPage,pageSize);
        return new RespEntity(RespCode.SUCCESS, result);
    }

    /**
     * 根据学校id查询所有未设置考勤规则的年级信息
     *
     * @param schoolId
     * @param request
     * @return
     */
    @GetMapping("/getGradeInfoBySchoolId/{schoolId}")
    public RespEntity getGradeInfoBySchoolId(@PathVariable("schoolId") Integer schoolId, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/getGradeInfoBySchoolId,schoolId={}", schoolId.toString());
        List<Map<String, Object>> result = attendanceRulesService.getUnBindingGradeBySchoolId(schoolId);
        return new RespEntity(RespCode.SUCCESS, result);
    }


    /**
     * 根据学校id查询所有有的勤规则信息
     *
     * @param schoolId
     * @param request
     * @return
     */
    @GetMapping("/getUnBindingRulesBySchoolId/{schoolId}")
    public RespEntity getUnBindingRulesBySchoolId(@PathVariable("schoolId") Integer schoolId, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/getUnBindingRulesBySchoolId,schoolId={}", schoolId.toString());
        List<Map<String, Object>> result = attendanceRulesService.getRulesBySchoolId(schoolId);
        return new RespEntity(RespCode.SUCCESS, result);
    }

    /**
     * 添加考勤设置
     *
     * @param data
     * @param request
     * @return
     */
    @PostMapping("/addAttendanceSetting")
    public RespEntity addAttendanceSetting(@RequestBody AttendanceSetting data, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/addAttendanceSetting,data={}", data.toString());
        //默认添加后打开
        data.setFlag(1);
        int i = attendanceRulesService.addAttendanceSetting(data);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 修改考勤设置
     *
     * @param data
     * @param request
     * @return
     */
    @PostMapping("/modifyAttendanceSetting")
    public RespEntity modifyAttendanceSetting(@RequestBody AttendanceSetting data, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/modifyAttendanceSetting,data={}", data.toString());
        int i = attendanceRulesService.modifyAttendanceSetting(data);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }


    /**
     * 考勤开关
     *
     * @param data
     * @param request
     * @return
     */
    @PostMapping("/modifyAttendanceSetFlag")
    public RespEntity modifyAttendanceSetFlag(@RequestBody AttendanceSetting data, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/modifyAttendanceSetFlag,data={}", data.toString());
        int flag = data.getFlag() == 0 ? 1 : 0;
        data.setFlag(flag);
        int i = attendanceRulesService.modifyAttendanceSetFlag(data);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 删除一笔考勤信息
     *
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping("/deleteAttendanceSet/{id}")
    public RespEntity deleteAttendanceSet(@PathVariable("id") Integer id, HttpServletRequest request) {
        Integer userId = getUserId(request);
        if (userId == null) {
            return new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        logger.info("/deleteAttendanceSet,id={}", id.toString());
        int i = attendanceRulesService.deleteAttendanceSetting(id);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }


    /**
     * 获取用户id
     *
     * @return
     */
    public Integer getUserId(HttpServletRequest request) {
        String token = request.getHeader(Constants.HEADER_TOKEN_KEY);
        //获取用户的id
        Map<String, Object> msg = redisService.getUserInfoByToken(token);
        String userMsg = (String) msg.get("user");
        JSONObject jsonObject = JSONObject.parseObject(userMsg);
        return (Integer) jsonObject.get("userId");
    }
}
